<template>
	<view class="goods">
		<view class="topSearch">
			<view class="left" @tap="navTo('/pages/lifeSearch/index')">
				<image src="/static/img/index/89.png" mode="widthFix"></image>
				搜索
			</view>
			<!-- <view class="right">
				<image src="/static/img/index/88.png" mode="widthFix"></image>
				打开地图
			</view> -->
		</view>
		<view class="topbar">
			<text @click="topTap(item)"
				:class="[data.obj.queryType == item.k?'active':'',item.k+'top']" v-for="(item,index) in data.topArr"
				:key="index">{{item.n}}</text>
		</view>
		<view class="topbar1">
			<text @tap="orderTypeChange('')" :class="!data.obj.orderType?'active':''">综合</text>
			<view class="view" @tap="orderTypeChange(item.k)" :class="data.obj.orderType == item.k?'active1':''"
				v-for="(item,index) in data.topArr1" :key="index">
				{{item.n}}
				<image
					:src="item.k == data.obj.orderType && data.obj.orderSort == 'ASC'?'/static/img/index/123.png':item.k == data.obj.orderType && data.obj.orderSort == 'DESC'?'/static/img/index/124.png':'/static/img/index/87.png'"
					mode="widthFix"></image>
			</view>
			<picker :range="data.distanceArr" range-key="n" @change="distanceChange">
				<view class="view">
					{{data.obj.lessDistance?data.distanceArr[data.distanceArr.map(item=>item.id).indexOf(data.obj.lessDistance)].n:'距离'}}
				</view>
			</picker>
			<picker v-if="data.obj.queryType == 'FREE'" :range="data.sellTypeArr" range-key="name" @change="sellTypeChange">
				<view class="view">
					{{data.obj.sellType?data.sellTypeArr[data.sellTypeArr.map(item=>item.id).indexOf(data.obj.sellType)].name:'筛选'}}
					<image src="/static/img/freeShipping/18.png" mode="widthFix"></image>
				</view>
			</picker>
		</view>
		<scroll-view :refresher-triggered="data.refresherBool" :refresher-enabled="true"
			@refresherrefresh="refresherrefresh" @scrolltolower="scrolltolower" scroll-y="true">
			<view class="go" @tap="handeClick">
				<view class="">
					点击查看免单购活动详情
					<text>Go</text>
				</view>
				
				<image src="/static/img/freeShipping/21.png" mode="aspectFill"></image>
			</view>
			<view class="list">
				<view @tap="navTo('/pages/lifestyleProducts/index?id='+item.id+'&productType='+item.productType+'&freeCode='+(props.freeCode || ''))"
					class="listItem" v-for="(item,index) in data.result.list" :key="index">
					<text  :class="item.sellType.value" class="leftTop xs">{{item.sellType.name}}</text>
					<text v-if="item.joinFreeFlag" class="topFreeText">免单购</text>
					<view class="img">
						<image :src="data.httpUrl+'/app/file/viewFile?fileId='+item.fileId" mode="aspectFill"></image>
					</view>
					<view class="des">{{item.productName}}</view>

					<view class="product-price" v-if="!item.preferentialPrice || item.preferentialPrice.length <=1">
						<view class="left">
							<text>￥{{item.price}}</text>
						</view>
						<text>已售: {{item.sellCount}}</text>
					</view>
					<view class="product-preferential-max" v-else-if="item.preferentialPrice.length > 5">
						<view class="left">
							<text>￥{{item.preferentialPrice}}</text>
							<text style="text-decoration:line-through;">¥{{item.price}}</text>
						</view>
						<text>已售: {{item.sellCount}}</text>
					</view>
					<view class="product-preferential" v-else>
						<view class="left">
							<text>￥{{item.preferentialPrice}}</text>
							<text style="text-decoration:line-through;">¥{{item.price}}</text>
						</view>
						<text>已售: {{item.sellCount}}</text>
					</view>

				</view>
			</view>
			<view class="zanwu" v-if="!data.result.list.length">
				<image src="/static/img/index/116.png" mode="widthFix"></image>
				暂无搜索结果
			</view>
		</scroll-view>
		<view class="card" @tap="navTo('/pages/shoppingCart/index')">
			<image src="/static/icons/shop/icons8-shopping-cart-64.png" mode="widthFix"></image>
			<text v-if="props.countNum">{{props.countNum}}</text>
		</view>
		<FreeFloating v-if="props.freeCode" :freeCode="props.freeCode" />
	</view>

</template>

<script lang="ts" setup>
	import { reactive } from 'vue';
	import { pageLifeProducts } from '@/utils/api/goods'
	import FreeFloating from '@/components/freeFloating.vue'
	import {
		setDialogView
	} from '@/utils/dialogView.ts'
	const props = defineProps(['countNum','freeCode','productType']);
	// , { n: '免单购', k: 'FREE' }
	const data = reactive<any>({
		topArr: [{ n: '推荐', k: '' }, { n: '免单购', k: 'FREE' },{ n: '线上', k: 'ONLINE' }, { n: '线下', k: 'OFFLINE' }, { n: '商品', k: 'PRODUCT' }, { n: '服务', k: 'SERVICE' }, { n: '需求', k: 'DEMAND' }],
		topArr1: [{ n: '销量', k: 'SELL_COUNT' }, { n: '价格', k: 'PRICE' }],
		// , { n: '距离', k: 'DISTANCE' }
		result: {},
		freeInfo:{},
		sellTypeArr:[{name:'全部',id:''},{name:'线上',id:'ONLINE'},{name:'线下',id:'OFFLINE'}],
		distanceArr: [{ n: '全部', id: '' }, { n: '附近', id: '100' }, { n: '1KM', id: '1000' }, { n: '5KM', id: '5000' }, { n: '10KM', id: '10000' }, { n: '15KM', id: '15000' },],
		httpUrl: getApp().globalData.baseUrl,
		obj: { freeCode:props.freeCode,orderType: '', queryType: props.freeCode || props.productType?'FREE':'', orderSort: '', productName: '', pageNo: 1, pageSize: 10 }
	})
	const navTo = (url : string) => {
		uni.navigateTo({
			url
		})
	}
	
	const orderTypeChange = (i) => {
		data.obj.pageNo = 1;
		if (!i) {
			data.obj.orderType = '';
			data.obj.orderSort = ''
		} else {
			if (i == data.obj.orderType) {
				data.obj.orderSort = data.obj.orderSort == 'ASC' ? 'DESC' : 'ASC'
			} else {
				data.obj.orderType = i;
				data.obj.orderSort = 'ASC'
			}

		}
		requstList()
	}
	const handeClick = () => {
		uni.navigateTo({
			url: '/pages/eventRegistrationNotice/index?type=buyer'
		})
	}
	const topTap = (item)=>{
		if(item.k != 'FREE'){
			data.obj.sellType = '';
		}
		data.obj.queryType = item.k;
		data.obj.pageNo = 1;
		requstList();
	}
	const distanceChange = (e) => {
		data.obj.lessDistance = data.distanceArr[e.detail.value].id;
		data.obj.pageNo = 1;
		requstList()
	}
	const sellTypeChange = (e)=>{
		data.obj.sellType = data.sellTypeArr[e.detail.value].id;
		data.obj.pageNo = 1;
		requstList()
	}
	const refresherrefresh = () => {
		data.refresherBool = true;
		data.obj.pageNo = 1;
		requstList()
	}
	const scrolltolower = () => {
		if (data.result.total != data.result.list.length) {
			data.obj.pageNo += 1;
			requstList()
		}
	}
	const requstList = () => {
		uni.showLoading({
			title: '加载中...',
			mask: true
		})
		if (data.obj.pageNo == 1) {
			data.result.list = [];
		}
		pageLifeProducts({ data: data.obj }).then((res) => {
			console.log(data.result.list)
			res.data.list.map((item) => {
				item.preferentialPrice = item.preferentialPrice + '';
				item.price = item.price + '';
				if (item.price.indexOf('.') == -1) {
					item.price = item.price + '.00'
				}
				if (item.preferentialPrice * 1 && item.preferentialPrice.indexOf('.') == -1) {
					item.preferentialPrice = item.preferentialPrice + '.00'
				}
			})
			res.data.list = [...data.result.list, ...res.data.list];
			data.result = res.data;
			data.refresherBool = false;
			uni.hideLoading()
		})
	}
	setDialogView('ACCESS_FINE_LOCATION').then((res) => {
		if (res == 1) {
			uni.getLocation({
				type: 'gcj02',
				success: function (res) {
					data.obj.latitude = res.latitude
					data.obj.longitude = res.longitude
				},
			});
		}
	})

	requstList()
</script>

<style scoped lang="scss">
	.go{
		margin: 12rpx 24rpx;
		border-radius: 24rpx;
		padding:12rpx 24rpx;
		position: relative;
		image{
			width: 100%;
			height: 100%;
			position: absolute;
			left: 0;
			right: 0;
			top: 0;
			bottom: 0;
		}
		view{
			display: flex;
			justify-content: space-between;
			align-items: center;
			color: #FF263F;
			font-size: 32rpx;
			position: relative;
			z-index: 2;
		}
		text{
			width: 156rpx;
			line-height: 72rpx;
			text-align: center;
			border-radius: 70rpx;
			color: #fff;
			font-size: 32rpx;
			font-weight: bold;
			background: linear-gradient(#FF8A33 0%, #FF4A4A 100%), #FF4A4A;
			box-shadow: inset 2rpx -2rpx 5rpx 0rpx rgba(255,255,255,0.4), inset -2rpx -2rpx 5rpx 0rpx rgba(255,255,255,0.4), inset -1rpx -1rpx 5rpx 0rpx rgba(255,255,255,0.4), inset 1rpx -1rpx 5rpx 0rpx rgba(255,255,255,0.4);
		}
	}
	.topFreeText{
		display: inline-block;
		width: 72rpx;
		line-height: 36rpx;
		text-align: center;
		border-radius: 0 24rpx 0rpx 24rpx;
		background: linear-gradient(324deg, #FFBE88 0%, #FF743A 100%), linear-gradient(24deg, rgba(255, 130, 150, 0) 0%, #FF6A82 100%);
		color: #FFF6E4;
		font-family: 'MyFont1';
		font-size: 20rpx;
		position: absolute;
		right: 0;
		z-index: 9;
		top: 0;
	}
	.card {
		position: fixed;
		right: 50rpx;
		bottom: 300rpx;
		z-index: 8;
		width: 80rpx;
		height: 80rpx;
		border-radius: 50%;
		background: #fff;
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 10;
		image {
			width: 50rpx;
		}
		
		text {
			width: 40rpx;
			height: 40rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			color: #fff;
			font-size: 20rpx;
			position: absolute;
			right: -10rpx;
			top: -10rpx;
			background: #FF5233;
			border-radius: 50%;
		}
	}

	scroll-view {
		flex: 1;
		overflow: auto;
	}

	.goods {
		flex: 1;
		height: 0;
		background: #F4F6FA;
		display: flex;
		flex-direction: column;
	}

	.OFFLINE {
		background: rgb(251, 180, 180) !important;
	}

	.ONLINE {
		background: rgb(120, 194, 253) !important;
	}

	.topbar {
		display: flex;
		padding: 0 32rpx;
		display: flex;
		justify-content: space-between;

		text {
			font-size: 32rpx;
		}

		.active {
			color: #6B64D3;
			font-size: 36rpx;
		}
	}

	.topSearch {
		padding: 0 32rpx;
		display: flex;
		height: 68rpx;
		margin: 41rpx 0;

		.left {
			background: #fff;
			border-radius: 68rpx;
			width: 100%;
			padding-left: 28rpx;
			display: flex;
			align-items: center;
			height: inherit;

			image {
				width: 28.8rpx;
				margin-right: 17rpx;
			}

			color: #8C8C8C;
			font-size: 24rpx;
		}

		.right {
			height: inherit;
			flex: 1;
			background: #fff;
			border-radius: 68rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			image {
				width: 48rpx;
				margin-right: 8rpx;
			}

			color: #8C8C8C;
			font-size: 24rpx;
		}
	}
	
	
	.list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 0 32rpx;

		>view:nth-of-type(1),
		>view:nth-of-type(2) {
			margin-top: 0;
		}

		.listItem {
			position: relative;
			margin-top: 28rpx;
			padding-bottom: 16rpx;
			border-radius: 24rpx;
			background: #fff;
			overflow: hidden;
			width: 48%;

			.img {
				height: 343rpx;
				background: #ccc;

				image {
					width: 100%;
					height: 100%;
				}
			}

			.leftTop {
				position: absolute;
				left: 0;
				top: 0;
				z-index: 9;
			}

			.xs {
				padding: 5rpx 16rpx;
				border-radius: 24rpx 0 24rpx 0;
				background: #CD9A67;
				color: #FFF5E2;
			}

			.des {
				margin: 8rpx 16rpx;
				overflow: hidden;
				display: -webkit-box;
				text-overflow: ellipsis; //属性规定当文本溢出包含元素时发生的事情  text-overflow: clip|ellipsis|string; (修剪/省略号/指定字符串)
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical
			}

			.product-price {
				margin: 0 16rpx 0 16rpx;
				display: flex;
				align-items: flex-end;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: flex-end;

					text:nth-of-type(1) {
						font-size: 36rpx;
						color: #FF5233;
						position: relative;
					}

					text:nth-of-type(2) {
						text-decoration: line-through;
						color: #BFBFBF;
						margin-left: 5rpx;
						font-size: 24rpx;
					}
				}

				>text {
					font-size: 26rpx;
					color: #8C8C8C;
				}
			}

			.product-preferential-max {
				margin: 0 16rpx 0 16rpx;
				display: flex;
				align-items: flex-end;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: flex-end;

					text:nth-of-type(1) {
						font-size: 30rpx;
						color: #FF5233;
						position: relative;
					}

					text:nth-of-type(2) {
						text-decoration: line-through;
						color: #BFBFBF;
						margin-left: 5rpx;
						font-size: 20rpx;
					}
				}

				>text {
					font-size: 22rpx;
					color: #8C8C8C;
				}
			}

			.product-preferential {
				margin: 0 16rpx 0 16rpx;
				display: flex;
				align-items: flex-end;
				justify-content: space-between;

				.left {
					display: flex;
					align-items: flex-end;

					text:nth-of-type(1) {
						font-size: 32rpx;
						color: #FF5233;
						position: relative;
					}

					text:nth-of-type(2) {
						text-decoration: line-through;
						color: #BFBFBF;
						margin-left: 5rpx;
						font-size: 20rpx;
					}
				}

				>text {
					font-size: 24rpx;
					color: #8C8C8C;
				}
			}
		}
	}

	.topbar1 {
		padding: 0 32rpx;
		display: flex;
		justify-content: space-between;
		margin-top: 30rpx;

		>text,
		.view {
			width: 120.6rpx;
			height: 58rpx;
			display: flex;
			align-items: center;
			border-radius: 58rpx;
			justify-content: center;

			image {
				width: 24rpx;
				margin-left: 8rpx;
			}
		}

		>text {
			// color: #fff;
			// background: #3D3F45;
			background: #fff;
		}

		.active {
			color: #fff;
			background: #3D3F45;
		}

		.view {
			background: #fff;
		}

		.active1 {
			color: #6B64D3;
		}
	}
	.FREEtop{
		font-family: 'MyFont1';
		color: #FF465B!important;
	}
	.zanwu {
		height: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		image {
			width: 238rpx;
			margin-bottom: 32rpx;
		}
	}
</style>